// Copyright (C) 2009  Yann de The
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#include "SEMisc.h"

std::string GetPathFromCFURLRef(CFURLRef urlRef) {
    char buffer[1024];
    std::string path;
    if(CFURLGetFileSystemRepresentation(urlRef, true, (UInt8*)buffer, 1024))
        path = std::string(buffer);
    return path;
}

std::string GetApplicationBundlePath() {
    // Get the main application bundle
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    
    std::string path;
    CFURLRef urlRef = CFBundleCopyBundleURL(mainBundle);
    if(urlRef)
    {
        path = GetPathFromCFURLRef(urlRef);
        CFRelease(urlRef); // docs say we are responsible for releasing CFURLRef
    }
    return path + "/";
    
}

std::string GetApplicationResourcesPath() {
    // Get the main application bundle
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    
    std::string path;
    CFURLRef urlRef = CFBundleCopyResourcesDirectoryURL(mainBundle);
    if(urlRef)
    {
        path = GetPathFromCFURLRef(urlRef);
        CFRelease(urlRef);
    }
    return path + "/";
}

vec3f cross(const vec3f& u, const vec3f& v) {
    vec3f res;
    
    return u.crossProduct(v);
}

void strip(std::string& str) {

    str.erase(str.begin());
    str.erase(str.end() - 1);
    
}

bool isURL(const std::string& str) {
    return str.substr(0,7) == "http://";
}

void takeScreenshot(irr::IrrlichtDevice* device)
{
    irr::video::IVideoDriver* const driver = device->getVideoDriver();
    
    //get image from the last rendered frame
    irr::video::IImage* const image = driver->createScreenShot();
    if (image) //should always be true, but you never know. ;)
    {
        //construct a filename, consisting of local time and file extension
        irr::core::stringw shot_path = "";
        
        shot_path += GetApplicationBundlePath().c_str();
        shot_path += "../screenshot_";
        shot_path += device->getTimer()->getRealTime();
        shot_path += ".jpg";
        
        //write screenshot to file
        if (!driver->writeImageToFile(image, shot_path.c_str()))
            device->getLogger()->log(L"Failed to take screenshot.", irr::ELL_WARNING);
        else
            device->getLogger()->log(L"Screenshot OK.", irr::ELL_INFORMATION);
        
        //Don't forget to drop image since we don't need it anymore.
        image->drop();
    }
    
}


